import Vue from "vue";
import VueRouter from "vue-router";
import { WalletList } from "@/utils/connect";
import { tryConnect, loadTSEPrice, loadGasPrice } from "@/init";
import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: () => import("../views/LandingPage/index.vue"),
  },
  {
    path: "/payment",
    name: "Payment",
    component: () => import("../views/Payment/index.vue"),
  },
  {
    path: "/airdrop",
    name: "Airdrop",
    component: () => import("../views/Airdrop/index.vue"),
  },
  {
    path: "/claim",
    name: "Claim",
    component: () => import("../views/Claim/index.vue"),
  },
  {
    path: "/price",
    name: "Price",
    component: () => import("../views/Price/index.vue"),
  },
  {
    path: "/dashboard",
    name: "Dashboard",
    component: () => import("../views/Dashboard/index.vue"),
  },
  {
    path: "*",
    redirect: "/",
  },
];

const router = new VueRouter({
  mode: "history",
  // base: process.env.BASE_URL,
  routes,
});

router.beforeEach(async (to, from, next) => {
  const { path } = to || {};
  if (path !== "/") {
    const { state } = store;
    const { chainCode, wallet } = state;
    const { code, connect } = wallet ? WalletList.find(item => {
      return item.code === wallet;
    }) : WalletList[0];
    tryConnect(chainCode, code, connect);
    loadGasPrice();
  }
  loadTSEPrice();
  return next();
});

export default router;
